package com.vim.modules.web.api;

import com.vim.common.annotation.ParentModuleInfo;
import com.vim.common.base.Constants;
import com.vim.common.exception.BusinessException;
import com.vim.common.model.exception.BusinessExceptionEnums;
import com.vim.common.model.response.RestfulResponse;
import com.vim.common.utils.IdGen;
import com.vim.common.utils.SendSms;
import com.vim.common.utils.ValidateUtils;
import com.vim.modules.web.model.UserInfo;
import com.vim.modules.web.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@ParentModuleInfo(moduleCode = "user", moduleName = "用户中心")
@Api(value = "用户信息管理", description = "获取用户信息以及登录注册")
@RestController
@RequestMapping(value = "/api/user/")
public class ApiUserInfoController {

    @Resource
    private UserInfoService userInfoService;

    @ApiOperation(value = "用户信息", notes = "{\"code\":20000,\"msg\":\"OK\",\"data\":{\"userId\":\"2796602e821144678e2084ce2f82a672\",\"userName\":\"448680\",\"userAvatar\":null,\"loginAccount\":\"15527047640\",\"loginPassword\":\"123456\",\"createTime\":1533989280000,\"token\":null}}", httpMethod="GET")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户ID", required = true)})
    @RequestMapping(value = "/userInfo")
    public RestfulResponse userInfo(@RequestParam(value = "userId") String userId){
        ValidateUtils.checkNotBlank(userId);
        return new RestfulResponse(userInfoService.find(userId));
    }

    @ApiOperation(value = "注册", notes = "{\"code\":20000,\"msg\":\"OK\",\"data\":\"OK\"}", httpMethod="POST")
    @ApiImplicitParams({@ApiImplicitParam(name = "loginAccount", value = "用户名", required = true),
                        @ApiImplicitParam(name = "loginPassword", value = "用户密码", required = true),
                        @ApiImplicitParam(name = "userName", value = "用户昵称", required = false)})
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public RestfulResponse register(@RequestParam(value = "loginAccount") String loginAccount,
                                    @RequestParam(value = "loginPassword") String loginPassword,
                                    @RequestParam(value = "userName", required = false) String userName){
        return new RestfulResponse(RestfulResponse.OK);
    }

    @ApiOperation(value = "账户登录", notes = "{\"code\":20000,\"msg\":\"OK\",\"data\":{\"userId\":\"2796602e821144678e2084ce2f82a672\",\"userName\":\"448680\",\"userAvatar\":null,\"loginAccount\":\"15527047640\",\"loginPassword\":null,\"createTime\":1533989280000,\"token\":\"58849fee7ea24037a42025f44c2faea5\"}}", httpMethod="POST")
    @ApiImplicitParams({@ApiImplicitParam(name = "loginAccount", value = "用户名", required = true),
                        @ApiImplicitParam(name = "loginPassword", value = "用户密码", required = true)})
    @RequestMapping(value = "/loginByAccount", method = RequestMethod.POST)
    public RestfulResponse loginByAccount(@RequestParam(value = "loginAccount") String loginAccount,
                                 @RequestParam(value = "loginPassword") String loginPassword){
        ValidateUtils.checkNotBlank(loginAccount);
        ValidateUtils.checkNotBlank(loginPassword);
        UserInfo userInfo = userInfoService.loginByAccount(loginAccount, loginPassword);
        return new RestfulResponse(userInfo);
    }

    @ApiOperation(value = "手机号登录", notes = "{\"code\":20000,\"msg\":\"OK\",\"data\":{\"userId\":\"2796602e821144678e2084ce2f82a672\",\"userName\":\"448680\",\"userAvatar\":null,\"loginAccount\":\"15527047640\",\"loginPassword\":null,\"createTime\":1533989280000,\"token\":\"b702667ec3344ecb99ded4558079623b\"}}", httpMethod="POST")
    @ApiImplicitParams({@ApiImplicitParam(name = "loginPhone", value = "手机号", required = true),
            @ApiImplicitParam(name = "loginCode", value = "验证码", required = true)})
    @RequestMapping(value = "/loginByPhone", method = RequestMethod.POST)
    public RestfulResponse loginByPhone(HttpSession session, @RequestParam(value = "loginPhone") String loginPhone,
                                          @RequestParam(value = "loginCode") String loginCode){
        ValidateUtils.checkNotBlank(loginPhone);
        ValidateUtils.checkNotBlank(loginCode);
        System.out.println(session.getId());
        Object object = session.getAttribute(Constants.PHONE_CODE);
        if(null == object){
            throw new BusinessException(BusinessExceptionEnums.ERROR_PHONE_LOGIN);
        }
        String code = object.toString();
        if(!code.equals(loginPhone+"_"+loginCode)){
            throw new BusinessException(BusinessExceptionEnums.ERROR_PHONE_LOGIN);
        }
        UserInfo userInfo = userInfoService.loginByPhone(loginPhone);
        return new RestfulResponse(userInfo);
    }

    @ApiOperation(value = "获取手机验证码", notes = "{\"code\":20000,\"msg\":\"OK\",\"data\":\"OK\"}", httpMethod="POST")
    @ApiImplicitParams({@ApiImplicitParam(name = "phone", value = "手机号码", required = true)})
    @RequestMapping(value = "/sendPhoneCode", method = RequestMethod.POST)
    public RestfulResponse sendPhoneCode(HttpSession session, @RequestParam(value = "phone") String phone){
        String code = IdGen.code();
        System.out.println(code);
        System.out.println(session.getId());
        boolean ok = SendSms.send(phone, code);
//        boolean ok = true;
        if(ok){
            session.setAttribute(Constants.PHONE_CODE, phone+"_"+code);
            return new RestfulResponse(RestfulResponse.OK);
        }else{
            return new RestfulResponse(RestfulResponse.SEND_PHONE_CODE_ERROR, "发送失败");
        }
    }
}
